package com.hzqy.web.ac.epg;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hzqy.service.epg.MovieAndSerialServiceBean;
import com.hzqy.web.login.SessionKey;
import com.hzqy.web.vo.activity.ActivityVoteUserVo;

@Controller
@RequestMapping("/")
public class MovieAndSerialAction {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	@Resource
	private MovieAndSerialServiceBean masService;
	
	
	/**
	 * 查询排行榜
	 * @param ccv
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping(value="/mas/movieandseriallist")
	@ResponseBody
	public Object selectMASList(String f_cc_obligate2,String f_avu_activityid) {
		Map<String,Object> map = new HashMap<String,Object>();
		map = masService.selectMASList(f_cc_obligate2,f_avu_activityid);
		return map;
	}
	/**
	 * 插入投票记录
	 * @param acvote
	 * @return
	 */
	@RequestMapping(value="/mas/insertMASVoteUser")
	@ResponseBody
	public Object insertMASVoteUser(ActivityVoteUserVo acvote,String serverRandom,HttpSession session) {
		Map<String,Object> mapReturn = new HashMap<String,Object>();
		String userid= (String) session.getAttribute(SessionKey.SESSION_SERVERID);
		String random=String.valueOf(session.getAttribute(userid+"random"));
		if (!random.equals(serverRandom)) {
			mapReturn.put("error", "RandomError");
			return mapReturn;
		}
		String tokenkey = HappyAction.token(userid);
		String attribute = String.valueOf(session.getAttribute("EPGverify"+userid));
		if (attribute=="null" || !attribute.equals(tokenkey)) {
				LOG.debug("恶意攻击接口,session未获取到EPGverify:" + attribute);
				mapReturn.put("error", "tokenError");
				return mapReturn;
		}
		String flag = masService.getStart();
		if ("start".equals(flag)) {
			acvote.setF_avu_serverid(userid);
			Map<String,Object> map = masService.selectMASVotenum(acvote);
			int result = 0;
			if (acvote.getF_avu_votenum() > (int)map.get("surplus") ) {
				result = -1;
			}else {
				acvote.setF_avu_type(2);
				acvote.setF_avu_source(0);
				acvote.setF_avu_memo("用户投票");
				acvote.setF_avu_obligate1(0);
				result = masService.insertMASVoteUser(acvote);
			}
			mapReturn.put("result", result);
		}
		mapReturn.put("time", flag);
		return mapReturn;
	}
	
	/**
	 * 查询票数
	 * @param avu
	 * @return
	 */
	@RequestMapping(value="/mas/selectMASVotenum")
	@ResponseBody
	public Object selectMASVotenum(ActivityVoteUserVo acvote,HttpSession session) {
		String userid= (String) session.getAttribute(SessionKey.SESSION_SERVERID);
		acvote.setF_avu_serverid(userid);
		Map<String,Object> map = new HashMap<String,Object>();
		map = masService.selectMASVotenum(acvote);
		return map;
	}
	
	/**
	 * 查询用户投票总数及排序
	 * @param acvote
	 * @return
	 */
	@RequestMapping(value="/mas/selectSisterVotenum")
	@ResponseBody
	public Object selectSisterVotenum(ActivityVoteUserVo acvote,HttpSession session) {
		Map<String,Object> map = new HashMap<String,Object>();
		ActivityVoteUserVo activityVoteUserVo= new ActivityVoteUserVo();
		String userid= (String) session.getAttribute(SessionKey.SESSION_SERVERID);
		acvote.setF_avu_serverid(userid);
		List<ActivityVoteUserVo> sisterList=masService.selectSisterVotenum(acvote);
		if (sisterList.size()>20) {
			map.put("alllist", sisterList.subList(0, 20));
		}else {
			map.put("alllist", sisterList);
		}
		
		for(int i=0;i<sisterList.size();i++) {
			activityVoteUserVo = sisterList.get(i);
			if(activityVoteUserVo.getF_avu_serverid().equals(acvote.getF_avu_serverid())) {
				map.put("mysize", i);
				map.put("result", activityVoteUserVo);
				break;
			}
		}
		
		return map;
	}
	
}
